/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ac.pbp.controller;

import ac.pbp.core.exception.ServiceException;
import ac.pbp.dto.filter.PengarangFilterCriteria;
import ac.pbp.core.util.ViewHelper;
import ac.pbp.dto.PengarangDto;
import ac.pbp.model.Buku;
import ac.pbp.model.Pengarang;
import ac.pbp.service.BukuService;
import ac.pbp.service.PengarangService;
import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.catalina.util.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author Kim
 */
@Controller
public class PengarangController {

    @Autowired
    private PengarangService PengarangService;

    @Autowired
    private BukuService bukuService;

    @RequestMapping("/pengarangView.html")
    public ModelAndView viewPengarang(HttpServletRequest request) {

        String page = request.getParameter("page");
        String mode = request.getParameter("mode");

        if ((page == null) || page.equals("")) {
            page = "1";
        }

        PengarangFilterCriteria filter = new PengarangFilterCriteria();
        filter.setPage(Integer.valueOf(page));
        filter.setMax(10);

        String dataSearch = null;
        if (mode != null) {
            if (mode.equals("search")) {
                dataSearch = request.getParameter("dataSearch");
                if (!dataSearch.equals("")) {
                    filter.setDataSearch(dataSearch);
                }
            }
        }

        PengarangDto PengarangDto = PengarangService.filterPengarang(filter);

        ModelAndView model = new ModelAndView("pengarangView");
        model.addObject("data", PengarangDto);
        model.addObject("dataSearch", dataSearch);

        return model;
    }

    @RequestMapping("/pengarangPrepareAdd.html")
    public ModelAndView prepareAddPengarang(HttpServletRequest request) {

        ModelAndView model = new ModelAndView("pengarangAdd");

        model.addObject("PengarangList", PengarangService.loadAll());

        return model;
    }

    @RequestMapping(value = "/pengarangAdd.html", method = RequestMethod.POST)
    public ModelAndView addPengarang(@ModelAttribute("Pengarang") Pengarang Pengarang, @RequestParam("fileGambar") MultipartFile fileGambar,
        HttpServletRequest request, HttpServletResponse response) throws ServiceException, IOException {

        String photo = Base64.encode(fileGambar.getBytes());
        
        PengarangService.addPengarang(Pengarang, photo);

        return ViewHelper.redirectTo("pengarangView.html");
    }

    @RequestMapping("/pengarangPrepareUpdate.html")
    public ModelAndView prepareUpdatePengarang(HttpServletRequest request) {

        String kodePengarang = request.getParameter("kodePengarang");

        ModelAndView model = new ModelAndView("pengarangAdd");
        Pengarang Pengarang = PengarangService.loadPengarang(kodePengarang);
        model.addObject("pengarang", Pengarang);
        model.addObject("pengarangList", PengarangService.loadAll());

        return model;
    }

    @RequestMapping(value = "/pengarangUpdate.html", method = RequestMethod.POST)
    public ModelAndView updatePengarang(@ModelAttribute("Pengarang") Pengarang Pengarang,@RequestParam("fileGambar") MultipartFile fileGambar,
    HttpServletRequest request, HttpServletResponse response) throws ServiceException, IOException {

        String photo = Base64.encode(fileGambar.getBytes());
        Pengarang.setPicture(photo);
        PengarangService.updatePengarang(Pengarang);

        return ViewHelper.redirectTo("pengarangView.html");
    }

    @RequestMapping("/pengarangDelete.html")
    public ModelAndView deletePengarang(HttpServletRequest request) {
        String idArray = request.getParameter("idArray");
        PengarangService.deletePengarang(idArray);

        return ViewHelper.redirectTo("pengarangView.html");
    }

    @RequestMapping("/detailPengarang.html")
    public ModelAndView detailPengarang(HttpServletRequest request) {

        String kodePengarang = request.getParameter("kodePengarang");

        ModelAndView model = new ModelAndView("detailPengarang");
        Pengarang Pengarang = PengarangService.loadPengarang(kodePengarang);
        List<Buku> bukuList = bukuService.loadBukuPengarang(kodePengarang);
        
        model.addObject("pengarang", Pengarang);
        model.addObject("bukuList", bukuList);
        
        return model;
    }
}
